﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using NTwitter;
using System.Collections.ObjectModel;
using System.Drawing;

namespace TwitterClient2.Components
{
    class PanelTweetOverview : Panel
    {
        private VScrollBar scrollbarTweetOverview;
        private List<PanelTweet> tweetChilds = new List<PanelTweet>();
        private int maximumLength = 0;
        private int height;
        private byte location;

        public PanelTweetOverview(int height, byte location)
        {
            this.location = location;
            this.height = height;
            InitializeComponent();

        }

        private void InitializeComponent()
        {
            this.scrollbarTweetOverview = new System.Windows.Forms.VScrollBar();
            this.SuspendLayout();

            // 
            // scrollbarTweetOverview
            // 
            this.scrollbarTweetOverview.Location = new System.Drawing.Point(210, 0);
            this.scrollbarTweetOverview.Name = "scrollbarTweetOverview";
            this.scrollbarTweetOverview.Size = new System.Drawing.Size(10, this.height);
            this.scrollbarTweetOverview.TabIndex = 0;
            this.scrollbarTweetOverview.ValueChanged += new System.EventHandler(this.scrollbarTweetOverview_ValueChanged);
            // 
            // PanelTweetOverview
            // 
            this.BackColor = System.Drawing.Color.Gainsboro;
            this.Controls.Add(this.scrollbarTweetOverview);
            this.Size = new System.Drawing.Size(220, this.height);
            this.ResumeLayout(false);

        }

        public void UpdateData(Collection<Status> c)
        {
            if (this.tweetChilds.Count > 0)
            {
                foreach (PanelTweet tweet in this.tweetChilds)
                {
                    this.Controls.Remove(tweet);
                }
            }
            this.tweetChilds = new List<PanelTweet>();
            int length = 5;
            foreach (Status status in c)
            {
                PanelTweet tweet = new PanelTweet(this.location);
                tweet.Location = new Point(5, length);
                DateTime date = status.CreatedAt;
                if (DateTime.Now.Day == date.Day && DateTime.Now.Month == date.Month && DateTime.Now.Year == date.Year)
                    tweet.labelDate.Text = "Today " + status.CreatedAt.Hour + ":" + status.CreatedAt.Minute;
                else
                    tweet.labelDate.Text = status.CreatedAt.Day + "/" + status.CreatedAt.Month + "/" + status.CreatedAt.Year;
                tweet.labelName.Text = status.User.ScreenName;
                tweet.labelText.Text = status.Text;
                int lengthText = status.Text.Length;
                int size;
                if (lengthText <= 40)
                    size = 40;
                else if (lengthText <= 80)
                    size = 60;
                else if (lengthText <= 120)
                    size = 80;
                else
                    size = 100;
                tweet.Size = new Size(200, size);
                length += size + 5;
                this.Controls.Add(tweet);
                this.tweetChilds.Add(tweet);
            }
            if (this.tweetChilds.Count > 0)
                this.maximumLength = length + this.tweetChilds[this.tweetChilds.Count - 1].Size.Height - 5;
            this.Refresh();
        }

        private void scrollbarTweetOverview_ValueChanged(object sender, EventArgs e)
        {
            int length = 5 - (int)(this.maximumLength * (this.scrollbarTweetOverview.Value / 100f));
            foreach (PanelTweet tweet in tweetChilds)
            {

                tweet.Location = new Point(5, length);
                int lengthText = tweet.labelText.Text.Length;
                int size;

                if (lengthText <= 40)
                    size = 40;
                else if (lengthText <= 80)
                    size = 60;
                else if (lengthText <= 120)
                    size = 80;
                else
                    size = 100;
                //i dont know why i did this!?!
                //tweet.Size = new Size(200, size);
                length += size + 5;
            }
            this.Refresh();
        }
    }
}
